package number_303;

public class NumArray {
    private final int[] preSumNums;
    public NumArray(int[] nums) {
        preSumNums = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            if (i == 0) {
                preSumNums[i] = nums[i];
            } else {
                preSumNums[i] = preSumNums[i - 1] + nums[i];
            }
        }
    }

    public int sumRange(int i, int j) {
        if (i > 0) {
            return preSumNums[j] - preSumNums[i - 1];
        } else {
            return preSumNums[j];
        }
    }

}
